<?php

/**
 * This is the view file.
 * It uses the control file to manipulate data
 *
 * @version 22 March 11
 * @author Aaron Jarzombek
 * @author Brice Rader
 */
echo "<html><body>";
include '../db-control/inc/functions.php';
include '../model/Organization.php';
include '../db-control/dbOrganization.php';

//update a organization if the user is trying to do so
if (isset($_POST['update']) && $_POST['update'] != '') {
    $id = $_POST['update'];
    $o = new Organization(); //creates a new instance of Organization()
    $o->setID($id); //sets the Organization ID
    $db = new dbOrganization();
    $exists = $db->checkID($o); //checks to see if the ID is a real organization
    if (!$exists) {
        echo "<b>Organization with ID '$id' does not exist...</b>";
    } else {
        $o->setName($_POST['name']); //sets the organization's name
        $o->setAddress($_POST['address']); //sets the organization's address
        $o->setAddress1($_POST['address1']); //sets the organization's address1
        $o->setCity($_POST['city']); //sets the organization's city
        $o->setState($_POST['state']); //sets the organization's state
        $o->setZip($_POST['zip']); //sets the organization's zip
        $result = $db->updateOrganization($o); //finally, updates the organization with the data we've set
        if (!$result) {
            echo "<b>Could not update organization with ID '$id'...</b>";
        }
    }
} else {
    if (isset($_POST['add'])) {
        $o = new Organization();
        $o->setName($_POST['name']);
        $o->setAddress($_POST['address']);
        $o->setAddress1($_POST['address1']);
        $o->setCity($_POST['city']);
        $o->setState($_POST['state']);
        $o->setZip($_POST['zip']);
        $db = new dbOrganization();
        $result = $db->addOrganization($o);
        if (!$result) {
            echo "<b>Please enter some organization information</b>";
        }
    }

    if (isset($_POST['deleteButton'])) {
        $o = new Organization(); //creates a new organization object (i.e. creates a new organization)
        $o->setID($_POST['deleteID']);
        $db = new dbOrganization();
        $result = $db->deleteOrganization($o);
        if (!$result) {
            echo "<b>Could not delete organization with ID '$_POST[deleteID]'...</b>";
        }
    }
}


//displays the web form used for adding a new organization
echo <<<_FORM


<form action='viewOrganization.php' method='post'>
<table>
    <tr><td colspan='2'>Specify an ID to change a organization's data</td><td><input type='text' size='4' name='update' /></td></tr>
    <tr><td>Name:</td><td><input type='text' value='' name='name' /></td></tr>
    <tr><td>Address:</td><td><input type='text' value='' name='address' /></td></tr>
    <tr><td>Address 1:</td><td><input type='text' value='' name='address1' /></td></tr>
    <tr><td>City:</td><td><input type='text' value='' name='city' /></td></tr>
    <tr><td>State:</td><td><input type='text' value='' name='state' /></td></tr>
    <tr><td>Zip:</td><td><input type='text' value='' name='zip' /></td></tr>
    <tr><td></td><td align='right'><input type='submit' value='Add Organization' name='add'/></td></tr>
</table>
</form>


_FORM;

displayOrganizations(); //displays the organization list


echo "</body></html>";

function displayOrganizations() {
    $db = new dbOrganization(); //gets all the organizations from the database
    $result = $db->getAllOrganizations();

    echo "<table width='200px'>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $id = $row['id'];
        $name = $row['name'];
        $address = $row['address'];
        $address1 = $row['address1'];
        $city = $row['city'];
        $state = $row['state'];
        $zip = $row['zip'];
        echo "<tr><td>$id</td><td></td></tr>";
        echo "<tr><td align='right'>Name:</td><td align='left'>$name</td></tr>";
        echo "<tr><td align='right'>Address:</td><td align='left'>$address</td></tr>";
        echo "<tr><td align='right'>Address 1:</td><td align='left'>$address1</td></tr>";
        echo "<tr><td align='right'>City:</td><td align='left'>$city</td></tr>";
        echo "<tr><td align='right'>State:</td><td align='left'>$state</td></tr>";
        echo "<tr><td align='right'>Zip:</td><td align='left'>$zip</td></tr>";
        echo "<tr><td colspan='2' align='right'><form action='viewOrganization.php' method='post'><input type='hidden' value='$id' name='deleteID' /><input type='submit' value='Delete' name='deleteButton' /></form></td><td></td></tr>";
        echo "<tr><td colspan='2'><hr /></td><td></td></tr>";
    }
    echo "</table>";
}
?>

